<template>
  <view>
    <u-line></u-line>
    <view style="padding: 30rpx" @click="toAlterCity(item.state)" v-for="(item,index) in stateList" :key="index">
      {{ item.state }}
    </view>
  </view>
</template>

<script>
import {alterFrontUserLocation, getCityList, getStateList} from "@/api/user";
import alterLocation from "@/pages/my/alterLocation.vue";

export default {
  data() {
    return {
      stateList: [],
      location: {
        county: '',
        province: '',
        city: ''
      }
    };
  },
  onLoad(options) {
    this.getStateList(options.country)
    uni.setNavigationBarTitle({title: options.country})
    this.location.country = options.country
  },
  methods: {
    async toAlterCity(state) {
      this.location.state = state
      const res = await getCityList()
      if (res.code == 200 && res.data.length > 0) {
        uni.navigateTo({
          url: `/pages/my/alterCity?country=${this.location.country}&state=${state}`
        })
      } else {
        const city = await alterFrontUserLocation(this.location)
        if (city.code == 200) {
          uni.$emit("alterUserInfo", {success: true})
          setTimeout(() => {
            uni.navigateBack({
              delta: 2
            })
          }, 500)
        } else {
          uni.showToast({
            title: '设置失败',
            duration: 2000,
            icon: 'none'
          });
        }
      }

    },
    async getStateList(country) {
      const res = await getStateList(country)
      this.stateList = res.data
    },
    async alterLocation() {
      const res = await alterFrontUserLocation()
      console.log(res)
    }
  }
}
</script>

<style lang="scss">

</style>
